
package biblio.service.hire;


import biblio.bean.HireBean;
import biblio.dao.BookDAO;
import biblio.dao.HireDAO;
import biblio.dao.ReaderDAO;
import biblio.domain.Book;
import biblio.domain.Hire;
import biblio.domain.Reader;
import biblio.exception.NoSuchEntityException;
import biblio.util.SourceName;
import biblio.util.interfaces.RequestOperationHelper;
import java.util.Date;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;


/**
 *
 * @author mephisto
 */
@ManagedBean(name="hireService")
@ApplicationScoped
public class HireServiceImpl implements HireService
{
    public void create()
    {
        RequestOperationHelper rOH = (RequestOperationHelper)appContext.getBean("rOH");
        hireBean = (HireBean) rOH.getBean("hireBean");
        Hire hire = new Hire();
        String[] readerTmp = hireBean.getReader().split("-");
        String[] bookTmp = hireBean.getBook().split("-");

        if (readerTmp.length != 2)
            throw new IllegalStateException("Illegal format for reader!");
        if (bookTmp.length != 2)
            throw new IllegalStateException("Illegal format for book!");

        BookDAO bookDAO = (BookDAO)appContext.getBean("bookDAO");
        ReaderDAO readerDAO = (ReaderDAO)appContext.getBean("readerDAO");
        Book book = null;
        Reader reader = null;
        try 
        {
            book = bookDAO.getById(Integer.parseInt(bookTmp[0]));
            reader = readerDAO.getById(Integer.parseInt(readerTmp[0]));
            log.debug("Reader " + reader);
            log.debug("Book " + book);
        }
        catch (NoSuchEntityException ex)
        {
           log.error(ex);
        }
        catch (NumberFormatException ex)
        {
           log.error("Identifier should be a number!", ex);
        }
        hire.setReader(reader);
        hire.setBook(book);
        hire.setHireDate(new Date());

        HireDAO hireDAO = (HireDAO)appContext.getBean("hireDAO");
        hireDAO.create(hire);
        hireBean.clear();
    }

    public void update(Hire hire)
    {
        log.error("Hire for update in service: " + hire);
        System.out.println("XXX-Hire for update: " + hire);
        HireDAO hireDAO = (HireDAO)appContext.getBean("hireDAO");
        try
        {
            hireDAO.update(hire);
        }
        catch (NoSuchEntityException ex)
        {
            log.error("Entity with id: " + hire.getId() + "does not exist in the database");
            log.error(ex.getMessage());
        }
    }

    public void delete(Hire hire)
    {
        HireDAO hireDAO = (HireDAO)appContext.getBean("hireDAO");
        try
        {
            hireDAO.delete(hire);
        }
        catch(DataAccessException e)
        {
            log.error("Entity with id: " + hire.getId() + "does not exist in the database");
            log.error(e);
        }
    }

    static
    {
        appContext = new ClassPathXmlApplicationContext( new String[] {
                SourceName.AUX_APPLICATION_CONTEXT,
                SourceName.DAO_APPLICATION_CONTEXT
        });
    }


    private static ApplicationContext appContext;
    private static Logger log = Logger.getLogger(HireServiceImpl.class);
    private HireBean hireBean;
}
